﻿/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#include "../import/head.h"
#include "../big_buf/head.h"
#include "../impl/head.h"
#include "../buf/head.h"
#include "msg_impl.h"

/////////////////////////////////////////////////////////////////////////////////////////
// 
namespace xos_box
{
    ////////////////////////////////////////////////////////////////////////////////////
    // 

    static msg_impl::POOL * pool_ptr = 0;

    ////////////////////////////////////////////////////////////////////////////////////

    msg_impl::msg_impl()
    {
        memset( m_lpRun, 0, sizeof( m_lpRun ) );
        memset( m_lpBigBuf, 0, sizeof( m_lpBigBuf ) );
        memset( m_lpBuf, 0, sizeof( m_lpBuf ) );
        memset( m_lpData, 0, sizeof( m_lpData ) );
        memset( m_dlData, 0, sizeof( m_dlData ) );
        memset( m_nData, 0, sizeof( m_nData ) );
        memset( m_i64, 0, sizeof( m_i64 ) );
        memset( m_bData, 0, sizeof( m_bData ) );
    }

    msg_impl::~msg_impl()
    {
        term();
    }

    ////////////////////////////////////////////////////////////////////////////////////
    // 

    int msg_impl::get_addr( POOL *** pppPool, LIST *** pppList )
    {
        int ret = 0;
        if( pppPool )
        {
            *pppPool = &pool_ptr;
        }
        if( pppList )
        {
            *pppList = 0;
        }
        return ret;
    }

    int msg_impl::init()
    {
        int nRet = 0;
        return nRet;
    }

    int msg_impl::term()
    {
        int nRet = 0;
        m_data_list.clear();
        m_str_map.clear();
        return nRet;
    }

    ////////////////////////////////////////////////////////////////////////////////////
    // 

    // 
    // go though
    // 
    void * msg_impl::begin()
    {
        void * p = 0;
        m_begin = m_data_list.begin();

        if( m_data_list.end() != m_begin )
        {
            p = *m_begin;
            m_begin++;
        }

        return p;
    }

    void * msg_impl::next()
    {
        void * p = 0;

        if( m_data_list.end() != m_begin )
        {
            p = *m_begin;
            m_begin++;
        }

        return p;
    }

    // 
    // front list operation
    // 
    int msg_impl::push_front( void * pData )
    {
        int nRet = 0;
        m_data_list.push_front( pData );
        return nRet;
    }

    void * msg_impl::front()
    {
        void * pRet = 0;
        
        if( m_data_list.size() > 0 )
        {
            pRet = m_data_list.front();
        }

        return pRet;
    }

    void msg_impl::pop_front()
    {
        m_data_list.pop_front();
    }

    // 
    // back list operation
    // 
    int msg_impl::push_back( void * pData )
    {
        int nRet = 0;
        m_data_list.push_back( pData );
        return nRet;
    }

    void * msg_impl::back()
    {
        void * pRet = 0;
        
        if( m_data_list.size() > 0 )
        {
            pRet = m_data_list.back();
        }

        return pRet;
    }

    void msg_impl::pop_back()
    {
        m_data_list.pop_back();
    }

    // 
    // string
    // 
    const char * msg_impl::get_str( int nIndex, char * lpszStr, int nLen, int * lpnLenNeeded )
    {
        const char * lpRet = m_strs[nIndex].c_str();

        if( lpnLenNeeded )
        {
            *lpnLenNeeded = ( int )strlen( lpRet );
        }

        if( lpszStr )
        {
            mgr::crt()->strcpy( lpszStr, nLen, lpRet );
        }

        return lpRet;
    }

    int msg_impl::set_str( int nIndex, const char * lpszStr )
    {
        int nRet = 0;

        if( lpszStr )
        {
			m_strs[nIndex] = lpszStr;
        }
        else
        {
			m_strs[nIndex] = "";
        }

        return nRet;
    }

    // 
    // map k-v. get ret 0 : success. 1 : failed
    //
    int msg_impl::get_map_value( const char * lpszKey, char * lpszValue, int nLen, int * lpnLenNeeded )
    {
        int nRet = 0;

        STR_ITER iter = m_str_map.find( lpszKey );
        if( iter != m_str_map.end() )
        {
            std::string & str = iter->second;
            mgr::crt()->strcpy( lpszValue, nLen, str.c_str() );
            if( lpnLenNeeded )
            {
                *lpnLenNeeded = ( int )str.size();
            }
        }
        else
        {
            nRet = 1;
        }

        return nRet;
    }

    int msg_impl::set_map_value( const char * lpszKey, const char * lpszValue )
    {
        int nRet = 0;
        m_str_map[lpszKey] = lpszValue;
        return nRet;
    }

    // 
    // xos_box::i_big_buf
    // 
    xos_box::i_big_buf * msg_impl::get_big_buf( int nIndex, xos_box::i_big_buf ** lppBigBuf )
    {
        xos_box::i_big_buf * lpBuf = m_lpBigBuf[nIndex];

        if( lppBigBuf )
        {
            *lppBigBuf = lpBuf;
        }

        return lpBuf;
    }

    int msg_impl::set_big_buf( int nIndex, xos_box::i_big_buf * lpBigBuf )
    {
        int nRet = 0;
        m_lpBigBuf[nIndex] = ( big_buf_impl * )lpBigBuf;
        return nRet;
    }

    // 
    // xos_box::i_buf
    // 
    xos_box::i_buf * msg_impl::get_buf( int nIndex, xos_box::i_buf ** lppBuf )
    {
        xos_box::i_buf * lpBuf = m_lpBuf[nIndex];

        if( lppBuf )
        {
            *lppBuf = lpBuf;
        }

        return lpBuf;
    }

    int msg_impl::set_buf( int nIndex, xos_box::i_buf * lpBuf )
    {
        int nRet = 0;
        m_lpBuf[nIndex] = ( buf_impl * )lpBuf;
        return nRet;
    }

    // 
    // double
    // 
    double msg_impl::get_double( int nIndex, double * lpdlData )
    {
        if( lpdlData )
        {
            *lpdlData = m_dlData[nIndex];
        }

        return m_dlData[nIndex];
    }

    int msg_impl::set_double( int nIndex, double dlData )
    {
        int nRet = 0;
        m_dlData[nIndex] = dlData;
        return nRet;
    }

    // 
    // void *
    // 
    void * msg_impl::get_void( int nIndex, void ** lppData )
    {
        void * pRet = m_lpData[nIndex];

        if( lppData )
        {
            *lppData = pRet;
        }

        return pRet;
    }

    int msg_impl::set_void( int nIndex, void * lpData )
    {
        int nRet = 0;
        m_lpData[nIndex] = lpData;
        return nRet;
    }

    // 
    // int
    // 
    int msg_impl::get_int( int nIndex, int * lpnInt )
    {
        int nRet = m_nData[nIndex];

        if( lpnInt )
        {
            *lpnInt = nRet;
        }

        return nRet;
    }

    int msg_impl::set_int( int nIndex, int nInt )
    {
        int nRet = 0;
        m_nData[nIndex] = nInt;
        return nRet;
    }

    // 
    // xos::xos_i64
    // 
    xos::xos_i64 msg_impl::get_i64( int nIndex, xos::xos_i64 * lpi64 )
    {
        if( lpi64 )
        {
            *lpi64 = m_i64[nIndex];
        }

        return m_i64[nIndex];
    }

    int msg_impl::set_i64( int nIndex, xos::xos_i64 i64 )
    {
        m_i64[nIndex] = i64;
        return 0;
    }

    // 
    // bool
    // 
    bool msg_impl::get_bool( int nIndex, bool * lpBool )
    {
        if( lpBool )
        {
            *lpBool = m_bData[nIndex];
        }

        return m_bData[nIndex];
    }

    int msg_impl::set_bool( int nIndex, bool bBool )
    {
        m_bData[nIndex] = bBool;
        return 0;
    }

    // 
    // xos::f_runnable
    // 
    xos::f_runnable msg_impl::get_runnable( int nIndex, xos::f_runnable * lpRun )
    {
        xos::f_runnable lpBuf = m_lpRun[nIndex];

        if( lpRun )
        {
            *lpRun = lpBuf;
        }

        return lpBuf;
    }

    int msg_impl::set_runnable( int nIndex, xos::f_runnable lpRun )
    {
        int nRet = 0;
        m_lpRun[nIndex] = lpRun;
        return nRet;
    }

} // xos
